package br.museu.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.museu.constantes.ConstantesModelAndView;
import br.museu.model.Usuario;

@Controller
@Scope("session")
public class HomeController {

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.index);
		return mv;
	}

	@RequestMapping(value = "home", method = RequestMethod.GET)
	public ModelAndView home(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.home);

		Usuario usuario = (Usuario) request.getSession().getAttribute("usuario");
		mv.addObject("usuario", usuario);

		return mv;
	}

	@RequestMapping(value = "cadastro", method = RequestMethod.GET)
	public ModelAndView cadastro() {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.cadastro);
		mv.addObject("usuario", new Usuario());
		return mv;
	}

	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.index);

		request.getSession().invalidate();

		return mv;
	}

}
